<?php
class ReaderTypeController extends Controller{
	public function indexAction(){
		$title="读者类型管理";
		$include_file="allreadertype";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		$readertypemodel=new ReaderTypeModel("tb_readertype");
		
		
		//定义每页数量
		$pagesize = 10;
		//接收页数参数,如果为空,则显示第一页
		$current = isset ( $_GET ['page'] ) ? $_GET ['page'] : 1;
		//计算偏移量
		$offset = ($current - 1) * $pagesize;
		//获取总条数,条件where为空
		$total = $readertypemodel->total("");
		$allreadertypes=$readertypemodel->getAllTypes($offset, $pagesize);
		//载入工具类,获取分页信息
		$this->library ( "Page" );
		$page=new Page($total, $pagesize, $current,"index.php",array("c"=>"reader","a"=>"index"));
		$pageinfo=$page->showPage();
		include VIEWS_PATH."index.html";
	}
	
	//添加读者类型
	public function addAction(){
		$title="添加读者类型";
		$include_file="addreadertype";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		include VIEWS_PATH."index.html";
	}
	
	//插入数据
	public function insertAction(){
		$data['name']=$_POST['name'];
		$data['number']=$_POST['number']+0;
		
		//判断是否为空
		if (empty($data['name'])) {
			$this->jump("index.php?c=readertype&a=add", "读者类型不能为空!");
		}
		if (empty($data['bumber'])){
			$data['munber']=1;
		}
		//转义+实体化
		$this->helper("input");
		$data=deepspecialchars($data);
		//实例化对象插入数据
		$readertypemodel=new ReaderTypeModel("tb_readertype");
		if ($readertypemodel->insert($data)) {
			$this->jump("index.php?c=readertype&a=index", "读者类型添加成功!",2);
		}else {
			$this->jump("index.php?c=readertype&a=add", "读者类型添加失败,请刷新后再试!",3);
		}
	}
	
	//修改
	public function editAction(){
		$title="修改读者类型";
		$include_file="editreadertype";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		$pk=$_GET['id'];
		$readertypemodel=new ReaderTypeModel("tb_readertype");
		$type=$readertypemodel->selectByPk($pk);
		include VIEWS_PATH."index.html";
	}
	
	//更新
	public function updateAction(){
		$data['name']=$_POST['name'];
		$data['id']=$_POST['id']+0;
		$data['number']=$_POST['number']+0;
		
		if (empty($data['name'])) {
			$this->jump("index.php?c=readertype&a=edit&id={$data['id']}", "读者类型名称不能修改为空!");
		}
		if (empty($data['id'])) {
			$this->jump("index.php?c=readertype&a=index", "参数不合法!");
		}
		if (empty($data['number'])) {
			$data['number']=1;
		}
		
		//转义+实体化
		$this->helper("input");
		$data=deepspecialchars($data);
		
		$readertypemodel=new ReaderTypeModel("tb_readertype");
		if ($readertypemodel->update($data)) {
			$this->jump("index.php?c=readertype&a=index", "读者类型修改成功!");
		}else {
			$this->jump("index.php?c=readertype&a=edit&id={$data['id']}", "读者类型修改失败,请刷新后再试!");
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}